//
//  LSRegisterVController.m
//  LookScore
//
//  Created by wr on 2017/12/7.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSRegisterVController.h"
#import "JKCountDownButton.h"
#import "NSString+JKNormalRegex.h"
#import "MBProgressHUD+MJ.h"
#import "LSHttpEnclosureTool.h"
#import "LSAccountCoder.h"
#import "LSAccountArchive.h"
#import "NSString+JKNormalRegex.h"
#import "LSSetNickNameVController.h"
#import "LSHttpEnclosureTool.h"
#import "LSUserAgreeViewController.h"
@interface LSRegisterVController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *verifitionTF;
@property (weak, nonatomic) IBOutlet UITextField *passwardTF;
@property (weak, nonatomic) IBOutlet UIButton *sureBu;
@property (weak, nonatomic) IBOutlet UIButton *directLoginBu;
@property (weak, nonatomic) IBOutlet UIButton *eyesBu;

@property (weak, nonatomic) IBOutlet JKCountDownButton *verificationBu;


@end

@implementation LSRegisterVController{
    BOOL isBind;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"注册";
    isBind = NO;
      self.edgesForExtendedLayout = NO;
    self.phoneTF.keyboardType=UIKeyboardTypePhonePad;
    self.passwardTF.keyboardType=UIKeyboardTypeASCIICapable;
    
    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [self.verificationBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];

    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSSubColor7ec5fd] forState:UIControlStateNormal];
    [self.sureBu setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forState:UIControlStateHighlighted];
    
    [self.directLoginBu setTitleColor:LSSubColor7ec5fd forState:UIControlStateNormal];
    [self.directLoginBu setTitleColor:LSNavMainColor forState:UIControlStateHighlighted];

    [self.verificationBu countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
        sender.enabled = NO;
        if (![self.phoneTF.text jk_isMobileNumber]) {
            [MBProgressHUD showOnlyMessage:@"请输入有效手机号" afterDelay:1.5f];
            sender.enabled = YES;
        }else{
 
        [sender startCountDownWithSecond:60];

        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
            NSString *title = [NSString stringWithFormat:@"剩余%zds",second];
            return title;
        }];
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            return @"重新获取";

        }];
            
        }
        
    }];
    
 
    

}

#pragma mark 🔥 事件
- (IBAction)verificationAction:(JKCountDownButton *)sender {
    
    [LSHttpEnclosureTool POST:LSUserCheckMobileIsBind parameters:@{@"mobile":_phoneTF.text} resBlock:^(id responseObject) {
        if (responseObject) {
            MyLog(@"111 --- %@",responseObject);
//            NSCFBoolean isBind = responseObject;

            if ([responseObject  isEqual: @(YES)]) {

                isBind = YES;
                
            }else{
                isBind = NO;
                [LSHttpEnclosureTool POST:LSUserValidateMobile parameters:@{@"mobile":_phoneTF.text} resBlock:^(id responseObject) {
                    if (responseObject) {
                        MyLog(@"注册验证码 -- %@",responseObject);
//                        [MBProgressHUD showOnlyMessage:@"获取成功" afterDelay:1.5f];
                    }
                }];
            }

        }
    }];
}

- (IBAction)protocolAction:(UIButton *)sender {
    LSUserAgreeViewController * userAgreeVC = [[LSUserAgreeViewController alloc] init];
    userAgreeVC.urlStr = @"http://www.kanbf.com/UserAgreement.html";
    userAgreeVC.titleStr = @"用户协议";
    [self.navigationController pushViewController:userAgreeVC animated:YES];
    
}
- (IBAction)privacyAction:(UIButton *)sender {
    LSUserAgreeViewController * userAgreeVC = [[LSUserAgreeViewController alloc] init];
    userAgreeVC.urlStr = @"http://www.kanbf.com/Privacypolicy.html";
    userAgreeVC.titleStr = @"隐私政策";
    [self.navigationController pushViewController:userAgreeVC animated:YES];
}



- (IBAction)sureAction:(UIButton *)sender {
    if (![self.phoneTF.text jk_isMobileNumber]) {
        [MBProgressHUD showOnlyMessage:@"请输入有效手机号" afterDelay:1.5f];
    }
    else if (self.verifitionTF.text.length<4||self.verifitionTF.text.length>6){
        [MBProgressHUD showOnlyMessage:@"请输入正确验证码" afterDelay:1.5f];
    }
    else if (isBind){
        [MBProgressHUD showOnlyMessage:@"该手机号已被注册" afterDelay:1.5f];
    }
    else if (self.passwardTF.text.length<6 || self.passwardTF.text.length > 15){
        [MBProgressHUD showOnlyMessage:@"请输入6-15位的密码" afterDelay:1.5f];
        
    }else if ([self isEmpty:self.passwardTF.text]) {
        [MBProgressHUD showOnlyMessage:@"密码中不能含有空格字符" afterDelay:1.5f];
    }
    else if (self.passwardTF.text.length >= 6 && self.passwardTF.text.length <= 15){
        
    NSDictionary *registDic=@{@"mobile":self.phoneTF.text,@"pwd":self.passwardTF.text,@"validataCode":self.verifitionTF.text,};
    [LSHttpEnclosureTool POST:LSUserRegist parameters:registDic resBlock:^(id responseObject) {
        if (responseObject) {
            [[NSUserDefaults standardUserDefaults] setObject:self.self.phoneTF.text forKey:@"userNameString"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passwordString"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"select"];
//             LSAccountCoder *coderModel=[LSAccountCoder mj_objectWithKeyValues:responseObject];
//            [LSAccountArchive saveAccount:coderModel];
            LSSetNickNameVController *nickNameVC=[[LSSetNickNameVController alloc]init];
            nickNameVC.cid=responseObject[@"Cid"];
            nickNameVC.type=KRevampOrSetNickNameIsSetting;
            
//            JANALYTICSRegisterEvent * event = [[JANALYTICSRegisterEvent alloc] init];
//
//            event.success = YES;
//
//            event.method = @"register type";
//
//            event.extra = @{@"custom key1":@"custom value"};
//
//            [JANALYTICSService eventRecord:event];
            
            [self.navigationController pushViewController:nickNameVC animated:YES];
            if (self.registeSuccessBlock) {
                self.registeSuccessBlock();
            }
    
        }
    }];
        
        
    }
    
}
-(BOOL)isEmpty:(NSString *) str {
    NSRange range = [str rangeOfString:@" "];
    if (range.location != NSNotFound) {
        return  YES; //yes代表包含空格
    }else {
        return  NO;  //反之
    }
}
- (IBAction)directLoginAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)eyesAction:(UIButton *)sender {
    sender.selected=!sender.selected;
    self.passwardTF.secureTextEntry = sender.selected;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
